package cn.lingyangwl.agile.pay.model.bo;

import cn.lingyangwl.framework.tool.core.exception.Assert;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

import java.util.HashMap;
import java.util.Map;

/**
 * @author shenguangyang
 */
@Getter
@Setter
@AllArgsConstructor
@Builder
public class PaySignData {
    public static final String KEY_TIMESTAMP = "reqTime";
    public static final String KEY_NONCE = "nonce";
    public static final String KEY_SIGN = "sign";
    public static final String KEY_SECRET = "secret";
    /**
     * url超时时间, 10s过期
     */
    public static final Integer URL_TIMEOUT = 10;
    /**
     * 时间戳
     */
    private Long timeStamp;
    /**
     * 随机数，前端生成随机数主要保证签名的多变性
     */
    private String nonce;
    /**
     * 签名
     * sign = md5(所有非空参数按照升续排序然后 + ts(当前时间戳) + nonce + 密钥)
     */
    private String sign;

    public void check() {
        // 判断需要的参数是否为空，不为空进行后续操作
        Assert.notEmpty(nonce, PaySignData.KEY_NONCE + " is null");
        Assert.notEmpty(sign, PaySignData.KEY_SIGN + " is null");
        Assert.notNull(timeStamp,  PaySignData.KEY_TIMESTAMP+ " is null");
    }

    /**
     * 转成map, 排除签名
     */
    public Map<String, Object> ofMap() {
        Map<String, Object> map = new HashMap<>();
        map.put(PaySignData.KEY_NONCE, this.getNonce());
        map.put(PaySignData.KEY_TIMESTAMP, this.getTimeStamp());
        return map;
    }
}
